package com.yipei.business.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlBtsPointImport;
import com.yipei.business.mapper.MlBtsPointImportMapper;
import com.yipei.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlBtsPointMapper;
import com.yipei.business.domain.MlBtsPoint;
import com.yipei.business.service.IMlBtsPointService;
import org.springframework.util.CollectionUtils;

/**
 * 标识BTSService业务层处理
 *
 * @author yipei
 * @date 2024-12-23
 */
@Service
public class MlBtsPointServiceImpl extends ServiceImpl<MlBtsPointMapper, MlBtsPoint> implements IMlBtsPointService
{
    @Autowired
    private MlBtsPointMapper mlBtsPointMapper;

    /**
     * 查询标识BTS
     *
     * @param id 标识BTS主键
     * @return 标识BTS
     */
    @Override
    public MlBtsPoint selectMlBtsPointById(Long id)
    {
        return mlBtsPointMapper.selectMlBtsPointById(id);
    }

    /**
     * 查询标识BTS列表
     *
     * @param mlBtsPoint 标识BTS
     * @return 标识BTS
     */
    @Override
    public List<MlBtsPoint> selectMlBtsPointList(MlBtsPoint mlBtsPoint)
    {
        return mlBtsPointMapper.selectMlBtsPointList(mlBtsPoint);
    }

    /**
     * 新增标识BTS
     *
     * @param mlBtsPoint 标识BTS
     * @return 结果
     */
    @Override
    public int insertMlBtsPoint(MlBtsPoint mlBtsPoint)
    {
        mlBtsPoint.setCreateTime(DateUtils.getNowDate());
        return mlBtsPointMapper.insertMlBtsPoint(mlBtsPoint);
    }

    /**
     * 修改标识BTS
     *
     * @param mlBtsPoint 标识BTS
     * @return 结果
     */
    @Override
    public int updateMlBtsPoint(MlBtsPoint mlBtsPoint)
    {
        mlBtsPoint.setUpdateTime(DateUtils.getNowDate());
        return mlBtsPointMapper.updateMlBtsPoint(mlBtsPoint);
    }

    /**
     * 批量删除标识BTS
     *
     * @param ids 需要删除的标识BTS主键
     * @return 结果
     */
    @Override
    public int deleteMlBtsPointByIds(Long[] ids)
    {
        return mlBtsPointMapper.deleteMlBtsPointByIds(ids);
    }

    /**
     * 删除标识BTS信息
     *
     * @param id 标识BTS主键
     * @return 结果
     */
    @Override
    public int deleteMlBtsPointById(Long id)
    {
        return mlBtsPointMapper.deleteMlBtsPointById(id);
    }

    @Override
    public void createBtsToTrading(String tdAcc, String symbol, int i) {
        DateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String now = formatter.format(date);
        List<MlBtsPoint> mlBtsPoints = this.lambdaQuery().eq(MlBtsPoint::getTdAcc,tdAcc)
                .eq(MlBtsPoint::getSymbol,symbol).eq(MlBtsPoint::getBsRq,now)
                .list();
        if (!CollectionUtils.isEmpty(mlBtsPoints)){
            MlBtsPoint mlBtsPoint = mlBtsPoints.get(0);
            if (mlBtsPoint.getBsLx().equals("B")&&i==1){
                mlBtsPoint.setBsLx("T");
                this.updateById(mlBtsPoint);
            }else if (mlBtsPoint.getBsLx().equals("S")&&i==0){
                mlBtsPoint.setBsLx("T");
                this.updateById(mlBtsPoint);
            }
        }else {
            MlBtsPoint mlBtsPoint = new MlBtsPoint();
            mlBtsPoint.setTdAcc(tdAcc);
            mlBtsPoint.setSymbol(symbol);
            mlBtsPoint.setBsRq(now);
            mlBtsPoint.setCreateTime(new Date());
          if (i==0){
              mlBtsPoint.setBsLx("B");
          }else if (i==1){
              mlBtsPoint.setBsLx("S");
          }
          this.save(mlBtsPoint);
        }
    }
}
